import {
  baseUrl
} from './api'
import Toast from '@vant/weapp/toast/toast';

export function getHeader() {
  const headerInfo = wx.getStorageSync('headerInfo')
  const header = {}
  header['client'] = 'weapp'
  if (headerInfo) {
    header[headerInfo.tokenHeader] = headerInfo.token
  }
  return header
}
export const myPost = (url, data, cb) => {

  const header = getHeader()
  header['Content-Type'] = 'application/json'
  wx.request({
    url: baseUrl + url,
    data: data,
    header: header,
    method: 'POST',
    success: function (res) {
      console.warn('-----------------myPost_start_success------------------\nurl:', baseUrl, url, '\n参数:', data, "\n数据:", res.data, '\n-----------------myPost_end_success--------------------\n ');
      if (res.data.status === 10010 || res.data.status !== 200) {
        try {
          Toast.fail(res.data.message)
        } catch (e) {
          wx.showToast({
            title: res.data.message,
            icon: 'error',
            duration: 3000
          })
        }
      }
      return typeof cb == "function" && cb(res.data)
    },
    fail: function (err) {
      console.warn('-----------------myPost_start_error------------------\nurl:', baseUrl, url, '\n参数:', data, "\n数据:", err, '\n-----------------myPost_end_error--------------------\n ');
      try {
        Toast.fail(error)
      } catch (e) {
        wx.showToast({
          title: error,
          icon: 'error',
          duration: 3000
        })
      }
      return typeof cb == "function" && cb(err)
    }
  })
}

export const myPut = (url, data, cb) => {
  const header = getHeader()
  header['Content-Type'] = 'application/json'
  wx.request({
    url: baseUrl + url,
    data: data,
    header: header,
    method: 'PUT',
    success: function (res) {
      console.warn('-----------------myPut_start_success------------------\nurl:', baseUrl, url, '\n参数:', data, "\n数据:", res.data, '\n-----------------myPut_end_success--------------------\n ');
      if (res.data.status === 10010 || res.data.status !== 200) {
        try {
          Toast.fail(res.data.message)
        } catch (e) {
          wx.showToast({
            title: res.data.message,
            icon: 'error',
            duration: 3000
          })
        }
      }
      return typeof cb == "function" && cb(res.data)
    },
    fail: function (err) {
      console.warn('-----------------myPut_start_error------------------\nurl:', baseUrl, url, '\n参数:', data, "\n数据:", err, '\n-----------------myPut_end_error--------------------\n ');
      try {
        Toast.fail(error)
      } catch (e) {
        wx.showToast({
          title: error,
          icon: 'error',
          duration: 3000
        })
      }
      return typeof cb == "function" && cb(err)
    }
  })
}

export const myGet = (url, data, cb) => {
  wx.request({
    url: baseUrl + url,
    data: data,
    header: getHeader(),
    method: 'get',
    success: function (res) {
      console.warn('-----------------myGet_start_success------------------\nurl:', baseUrl, url, '\n参数:', data, "\n数据:", res.data, '\n-----------------myGet_end_success--------------------\n ');
      if (res.data.status === 10010 || res.data.status !== 200) {
        try {
          Toast.fail(res.data.message)
        } catch (e) {
          wx.showToast({
            title: res.data.message,
            icon: 'error',
            duration: 3000
          })
        }
      }
      return typeof cb == "function" && cb(res.data)
    },
    fail: function (err) {
      console.warn('-----------------myGet_start_error------------------\nurl:', baseUrl, url, '\n参数:', data, "\n数据:", err, '\n-----------------myGet_end_error--------------------\n ');
      try {
        Toast.fail(error)
      } catch (e) {
        wx.showToast({
          title: error,
          icon: 'error',
          duration: 3000
        })
      }
      return typeof cb == "function" && cb(err)
    }
  })
}

export const promiseGet = (url, data) => {
  return new Promise((resolve, reject) => wx.request({
    url: baseUrl + url,
    data: data,
    header: getHeader(),
    method: 'get',
    success: function (res) {
      console.warn('-----------------promiseGet_start_success------------------\nurl:', baseUrl, url, '\n参数:', data, "\n数据:", res.data, '\n-----------------promiseGet_end_success--------------------\n ');
      if (res.data.status !== 200) {
        try {
          Toast.fail(res.data.message)
        } catch (e) {
          wx.showToast({
            title: res.data.message,
            icon: 'error',
            duration: 3000
          })
        }
      }
      resolve(res.data)
    },
    fail: function (err) {
      console.warn('-----------------promiseGet_start_error------------------\nurl:', baseUrl, url, '\n参数:', data, "\n数据:", err, '\n-----------------promiseGet_end_error--------------------\n ');
      try {
        Toast.fail(error)
      } catch (e) {
        wx.showToast({
          title: error,
          icon: 'error',
          duration: 3000
        })
      }
      reject(err)
    }
  }));
}

const promisic = function (func) {
  return function (params = {}) {
    return new Promise((resolve, reject) => {
      const args = Object.assign(params, {
        success: (res) => {
          if (res.data.status != 200) {
            try {
              Toast.fail(res.data.message)
            } catch (e) {
              wx.showToast({
                title: res.data.message,
                icon: 'error',
                duration: 3000
              })
            }
          }
          console.warn('-----------------asyncRequest_start_success------------------\nurl:', params.url, '\n参数:', params.data, "\n数据:", res.data, '\n-----------------asyncRequest_end_success--------------------\n ');
          resolve(res);
        },
        fail: (error) => {
          try {
            Toast.fail(error)
          } catch (e) {
            wx.showToast({
              title: error,
              icon: 'error',
              duration: 3000
            })
          }
          console.warn('-----------------asyncRequest_start_success------------------\nurl:', baseUrl, url, '\n参数:', data, "\n数据:", res.data, '\n-----------------asyncRequest_end_success--------------------\n ');
          reject(error);
        }
      });
      func(args);
    });
  };
};


export class Http {
  // 同步Http请求
  static async asyncRequest(url, method, data, backMethod) {
    let res = await promisic(wx.request, url)({
      url: baseUrl + url,
      method: method,
      header: getHeader(),
      data: data,
    })
    backMethod(res.data)
  }
}